<!-- subpkg_medicine/payment/payment.vue -->
<script lang="ts" setup>
	import { ref } from 'vue'
	import { addressListApi, preOrderApi, createOrderApi } from '@/apis/order'
	import { orderPayApi } from '@/apis/consult'

	import { Address, OrderPre } from '@/types/order.d.ts'

	// 地址
	const addInfo = ref({} as Address)

	const getAddress = async () => {
		const res = await addressListApi()
		// 获取默认的地址信息
		addInfo.value = res[0]
	}
	getAddress()

	// 预支付订单
	const props = defineProps<{ id: string }>()
	const orderInfo = ref({} as OrderPre)
	const getOrderPre = async () => {
		const res = await preOrderApi(props.id)
		// console.log(res)
		orderInfo.value = res
	}
	getOrderPre()

	// 支付渠道选择
	const paymentRef = ref()

	// 显示
	const orderId = ref('')
	const showPayment = async () => {
		if (orderId.value === '') {
			const res = await createOrderApi(props.id, addInfo.value.id)
			console.log(res)
			orderId.value = res.id
			// 创建订单 再显示页面
			paymentRef.value.open()
		}
	}

	// 关闭
	const closePayment = () => {
		paymentRef.value.close()
	}

	// 立即支付
	const confirmOrder = async (paymentType: number) => {
		// console.log(paymentType)
		if (paymentType !== 0) {
			return uni.showToast({
				title: '目前只支持支付宝支付',
			})
		}

		const res = await orderPayApi({
			orderId: orderId.value,
			paymentMethod: '1',
			// 问诊支付完毕去看医生
			// 处方支付完毕去看支付结果
			payCallback: 'http://localhost:5173/#/subpkg_medicine/pay_result/pay_result',
		})
		// console.log(res)
		// 代码跳转页面
		window.location.href = res.payUrl
	}
</script>

<template>
	<scroll-page background-color="#f6f6f6">
		<view class="medicine-page">
			<view class="page-header">
				<view class="order-shippment">
					<view class="region">
						<uni-icons size="16" color="#FF7702" type="location-filled" />
						{{ addInfo.province + addInfo.city + addInfo.county }}
					</view>
					<view class="detail">{{ addInfo.addressDetail }}</view>
					<view class="receiver">{{ addInfo.receiver }} {{ addInfo.mobile }}</view>
				</view>
			</view>

			<view class="order-shop">
				优医药房
				<text class="alt">优医质保 假一赔十</text>
			</view>
			<!-- 药品列表 -->
			<view class="medicine-list">
				<view v-for="item in orderInfo.medicines" :key="item.id" class="medicine-list-item">
					<image class="medicine-cover" :src="item.avatar" mode="aspectFill" />
					<view class="medicine-info">
						<text class="name">{{ item.name }}</text>
						<text class="unit symbol">{{ item.specs }}</text>
						<text class="price">¥{{ item.amount }}</text>
					</view>
					<view class="quantity">x{{ item.quantity }}</view>
					<view class="guide">{{ item.usageDosag }}</view>
				</view>
			</view>

			<!-- 订单信息 -->
			<view class="order-info">
				<uni-list :border="false">
					<uni-list-item title="药品金额" :right-text="`¥${orderInfo.payment}`" />
					<uni-list-item title="运费" :right-text="`¥${orderInfo.expressFee}`" />
					<uni-list-item
						title="优惠券"
						show-arrow
						:right-text="`-¥${orderInfo.pointDeduction}`"
					/>
					<uni-list-item title="实付款" :right-text="`¥${orderInfo.actualPayment}`" />
				</uni-list>
			</view>

			<!-- 底部 -->
			<view class="toolbar">
				<view class="total-amount">
					需付款: <text class="number">¥{{ orderInfo.actualPayment }}</text>
				</view>
				<view class="buttons">
					<button @tap="showPayment" class="uni-button">立即支付</button>
				</view>
			</view>
		</view>
	</scroll-page>
	<custom-payment
		ref="paymentRef"
		@close="closePayment"
		:amount="orderInfo.actualPayment"
		@confirm="confirmOrder"
	></custom-payment>
</template>

<style lang="scss">
	@import './index.scss';
</style>
